package com.midea.ai.air.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ActionBarBackFragment;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.ConsVal;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.common.Global;
import com.midea.ai.air.ui.home.fragment.EditRoomFragment;
import com.midea.ai.air.ui.home.fragment.WeatherCitiesFragment;
import com.midea.ai.air.ui.home.fragment.WeatherFragment;
import com.midea.ai.air.utils.ComUtils;

/**
 * Created by ouyangdi on 2016/3/31.
 * 不用滑动的Activity
 */
public class ActivityNotSlip extends BaseActivity {

    private String mStrTitle;
    private String mBtnText;
    private int mPageTag;
    private FrameLayout mLayoutContent;
    private WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_base_not_slip);

        mLayoutContent = (FrameLayout)findViewById(R.id.content_layout);

        getIntentData();
        initActionBar();
        addContentFragment();
    }

    /**
     * 获取Intent携带的数据
     */
    private void getIntentData() {
        mStrTitle = getIntent().getStringExtra(ConsVal.KEY_TITLE);
        mBtnText = getIntent().getStringExtra(ConsVal.KEY_BACK_TEXT);
        mPageTag = getIntent().getIntExtra(ConsVal.KEY_PAGE_TAG,-1);
    }

    /**
     * 初始化顶部工具栏
     */
    private void initActionBar() {
        FrameLayout mBarLayout = (FrameLayout)findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);

        ActionBarBackFragment backFragment = (ActionBarBackFragment)getSupportFragmentManager().findFragmentById(R.id.bar_fragment);
        backFragment.getBackButton().setText(mBtnText);
        backFragment.getTvTitle().setText(mStrTitle);

        switch (mPageTag) {
            case ConsVal.ACTIVITY_TAG_WEATHER:
                backFragment.getBarFullLayout().setBackgroundColor(getResources().getColor(R.color.bg_weather_sun));
                backFragment.getBackButton().setTextColor(getResources().getColor(R.color.common_white));
                backFragment.getTvTitle().setVisibility(View.GONE);
                backFragment.getBotLine().setVisibility(View.GONE);
                backFragment.setBackLeftIcon(R.drawable.air_action_bar_left_white);
                break;
        }
    }

    private void addContentFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        switch (mPageTag) {
            case ConsVal.ACTIVITY_TAG_WEATHER:
                WeatherFragment weatherFragment = new WeatherFragment();
                ft.add(R.id.content_layout, weatherFragment);
                ft.commit();
                break;

            case ConsVal.ACTIVITY_TAG_EDIT_ROOM:
                EditRoomFragment editRoomFragment= new EditRoomFragment();
                ft.add(R.id.content_layout, editRoomFragment);
                ft.commit();
                break;

            case ConsVal.ACTIVITY_TAG_HOME_WEATHER_CITIES:
                WeatherCitiesFragment citiesFragment= new WeatherCitiesFragment();
                ft.add(R.id.content_layout, citiesFragment);
                ft.commit();
                break;

            case ConsVal.ACTIVITY_TAG_MALL_FAN:
                initWebView();
                mLayoutContent.addView(mWebView);
                if(mStrTitle.equals(getString(R.string.air_more_mall))) {
                    mWebView.loadUrl(Global.shopUrl);
                } else {
                    mWebView.loadUrl(Global.mideaFansUrl);
                }
                break;

            default:
                break;
        }
    }

    private void initWebView() {

        FrameLayout.LayoutParams mLP = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        mWebView = new WebView(getContext());
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.setLayoutParams(mLP);
        String str = mWebView.getSettings().getUserAgentString().replace("Chrome", "Midea").replace("Firefox", "Midea");
        /**
         * 修改userAgent以适配美的商城的显示
         */
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setUserAgentString(str);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.setHorizontalScrollBarEnabled(false);//水平不显示
        mWebView.setVerticalScrollBarEnabled(false); //垂直不显示

        //网页正在加载时显示当前加载进度
/*        final ProgressDialog dialog = new ProgressDialog(ActivityNotSlip.this);
        dialog.setMessage("正在加载……0%");
        dialog.show();

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if(newProgress != 100) {
                    if(dialog.isShowing()) {
                        dialog.setMessage("正在加载……" + newProgress + "%");
                    }
                } else if(dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });*/
    }

    //点击EditText以外的区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (ComUtils.isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onStop() {
        if(mWebView != null) {
            mWebView.clearCache(true);
        }
        super.onStop();
    }

}
